package com.yanqu.road.dao.impl.activity.stelesoul;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.stelesoul.SteleSoulMapData;
import com.yanqu.road.entity.activity.stelesoul.SteleSoulUserData;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SteleSoulUserDataDaoImpl extends TempDataObjectDao<SteleSoulUserData> {
    public static String getSteleSoulUserDataSql = "select * from `t_u_stele_soul_user` where `activityId`=? and `userId`=?";

    public SteleSoulUserData getSteleSoulUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSteleSoulUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateSteleSoulUserDataSql = "update `t_u_stele_soul_user` set round=?,circle=?,mapData=?,paramOne=?,paramTwo=?,lastIndex=?,chosenReward=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(SteleSoulUserData steleSoulUserData) {
        steleSoulUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, steleSoulUserData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, steleSoulUserData.getCircle()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(steleSoulUserData.getMapData())));
        para.put(i++, new DbParameter(Types.INTEGER, steleSoulUserData.getParamOne()));
        para.put(i++, new DbParameter(Types.INTEGER, steleSoulUserData.getParamTwo()));
        para.put(i++, new DbParameter(Types.VARCHAR, steleSoulUserData.getLastIndex()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(steleSoulUserData.getChosenReward())));
        para.put(i++, new DbParameter(Types.INTEGER, steleSoulUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, steleSoulUserData.getUserId()));
        if (getDb().executeNoneQuery(updateSteleSoulUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSteleSoulUserDataSql = "insert into `t_u_stele_soul_user`(activityId,userId,round,circle,mapData,paramOne,paramTwo,lastIndex,chosenReward) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SteleSoulUserData steleSoulUserData) {
        steleSoulUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, steleSoulUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, steleSoulUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, steleSoulUserData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, steleSoulUserData.getCircle()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(steleSoulUserData.getMapData())));
        para.put(i++, new DbParameter(Types.INTEGER, steleSoulUserData.getParamOne()));
        para.put(i++, new DbParameter(Types.INTEGER, steleSoulUserData.getParamTwo()));
        para.put(i++, new DbParameter(Types.VARCHAR, steleSoulUserData.getLastIndex()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(steleSoulUserData.getChosenReward())));
        if (getDb().executeNoneQuery(insertSteleSoulUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SteleSoulUserData parseData(ResultSet rs) throws SQLException {
        SteleSoulUserData steleSoulUserData = new SteleSoulUserData();
        steleSoulUserData.setActivityId(rs.getInt("activityId"));
        steleSoulUserData.setUserId(rs.getLong("userId"));
        steleSoulUserData.setRound(rs.getInt("round"));
        steleSoulUserData.setCircle(rs.getInt("circle"));
        steleSoulUserData.setMapData(JSON.parseObject(rs.getString("mapData"), new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.activity.stelesoul.SteleSoulMapData>>() {}));
        steleSoulUserData.setParamOne(rs.getInt("paramOne"));
        steleSoulUserData.setParamTwo(rs.getInt("paramTwo"));
        steleSoulUserData.setLastIndex(rs.getString("lastIndex"));
        String chosenReward = rs.getString("chosenReward");
        if (!StringUtils.isNullOrEmpty(chosenReward)) {
            steleSoulUserData.setChosenReward(JSON.parseObject(chosenReward, new TypeReference<ConcurrentHashMap<Integer, List<Property>>>() {}));
        }

        return steleSoulUserData;
    }
}
